Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Polymorphism in java

Superclass & subclass

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create an instance of a subclass, an instance of the parent class is created implicitly, which is referred to by the super reference variable. Here are different ways to use the super keyword: Refer Immediate Parent Class Instance Variable If the parent class and child class have the same fields, we can use the super keyword to access the data member or field of the parent class.
Super class example in java class Animal { String color = "white"; } class Dog extends Animal { String color = "black"; void printColor() { System.out.println(color); // prints color of Dog class System.out.println(super.color); // prints color of Animal class } } class Main { public static void main(String[] args) { Dog obj = new Dog(); obj.printColor(); } }

Output

black white
Invoke Parent Class Method : The super keyword can also be used to invoke the parent class method. This is useful when the subclass contains the same method as the parent class.
Invoking parent class method example using super keyword in java class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void eat() { System.out.println("eating bread..."); } void bark() { System.out.println("barking..."); } void work() { super.eat(); bark(); } } class Main { public static void main(String[] args) { Dog obj = new Dog(); obj.eat(); obj.bark(); obj.work(); } }

Output

eating bread... barking... eating... barking...
Invoke Parent Class Constructor : The super keyword can also be used to invoke the parent class constructor.
Invoking parent class constructor example using super keyword in java class Animal { Animal() { System.out.println("animal is created"); } } class Dog extends Animal { Dog() { super(); System.out.println("dog is created"); } } class Main { public static void main(String[] args) { Dog obj = new Dog(); } }

Output

animal is created dog is created
Subclass : Method overriding in Java is when a subclass provides a specific implementation for a method that is already defined in its superclass. It enables us to customize the behavior of a method in the subclass while maintaining the same method signature as in the superclass. Here’s an example:
Subclass example in java - subclass printing first class Vehicle { void run() { System.out.println("Vehicle is running"); } } class Bike extends Vehicle { void run() { //super.run(); //subclass will be implemented first if super keyword is not used System.out.println("Bike is running safely"); } } class Main { public static void main(String[] args) { Bike obj = new Bike(); obj.run(); } }

Output

Bike is running safely
In this example, the run method in the Bike class overrides the run method in the Vehicle class. These two concepts are fundamental to Java’s OOP and are used extensively in Java programming. They enhance the flexibility and reusability of code, and allow us to design more efficient and effective programs.

  📌TAGS

★Class ★ Method ★ Object ★ java ★ oops ★ Polymorphism ★ Superclass ★ subclass

Tutorials